﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;

namespace TbShop.Web.Infrastructure.Tbk
{
    public class ResponseHelper
    {
        public static HttpResponseMessage Success(object obj, string message = "Success")
        {
            if (obj == null || (obj.GetType().Name.Equals("String", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(obj.ToString())))
            {
                var temp = new { Status = ResultStauts.Success, Message = message };
                return Base(temp, message);
            }
            else
            {
                var temp = new { Status = ResultStauts.Success, Message = message, Data = obj };
                return Base(temp, message);
            }
        }

        public static HttpResponseMessage Fail(object obj, string message = "fail")
        {
            if (obj == null || (obj.GetType().Name.Equals("String", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(obj.ToString())))
            {
                var temp = new { Status = ResultStauts.Fail, Message = message };
                return Base(temp, message);
            }
            else
            {
                var temp = new { Status = ResultStauts.Fail, Message = message, Data = obj };
                return Base(temp, message);
            }
        }

        public static HttpResponseMessage Invalid(object obj, string message = "Invalid")
        {
            if (obj == null || (obj.GetType().Name.Equals("String", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(obj.ToString())))
            {
                var temp = new { Status = ResultStauts.Invalid, Message = message };
                return Base(temp, message);
            }
            else
            {
                var temp = new { Status = ResultStauts.Invalid, Message = message, Data = obj };
                return Base(temp, message);
            }
        }

        public static HttpResponseMessage UnAuthorized(object obj, string message = "UnAuthorized")
        {
            if (obj == null || (obj.GetType().Name.Equals("String", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(obj.ToString())))
            {
                var temp = new { Status = ResultStauts.UnAuthorized, Message = message };
                return Base(temp, message);
            }
            else
            {
                var temp = new { Status = ResultStauts.UnAuthorized, Message = message, Data = obj };
                return Base(temp, message);
            }
        }

        public static HttpResponseMessage IntError(object obj, string message = "IntError")
        {
            if (obj == null || (obj.GetType().Name.Equals("String", StringComparison.OrdinalIgnoreCase) && string.IsNullOrEmpty(obj.ToString())))
            {
                var temp = new { Status = ResultStauts.IntError, Message = message };
                return Base(temp, message);
            }
            else
            {
                var temp = new { Status = ResultStauts.IntError, Message = message, Data = obj };
                return Base(temp, message);
            }
        }

        public static HttpResponseMessage Base(object obj, string message)
        {
            string str = JsonConvert.SerializeObject(obj);
            HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
            return result;
        }
    }

    public class ResultStauts
    {
        public const int Success = 200;
        public const int Invalid = 404;
        public const int IntError = 500;
        public const int UnAuthorized = 302;
        public const int Fail = 501;
    }
}